package com.xiaoq.coms.util;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class NetUtil {
	
	/**
	 *  获取用户的真实IP
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public static final String getIpAddr(final HttpServletRequest request)  
            throws Exception { 
		
        if (request == null) {  
            throw (new Exception("getIpAddr method HttpServletRequest Object is null"));  
        }  
        String ipString = request.getHeader("x-forwarded-for");  
        if (StringUtils.isBlank(ipString) || "unknown".equalsIgnoreCase(ipString)) {  
            ipString = request.getHeader("Proxy-Client-IP");  
        }  
        if (StringUtils.isBlank(ipString) || "unknown".equalsIgnoreCase(ipString)) {  
            ipString = request.getHeader("WL-Proxy-Client-IP");  
        }  
        if (StringUtils.isBlank(ipString) || "unknown".equalsIgnoreCase(ipString)) {  
            ipString = request.getRemoteAddr();  
        }  
           
        // 多个路由时，取第一个非unknown的ip  
        final String[] arr = ipString.split(",");  
        for (final String str : arr) {  
            if (!"unknown".equalsIgnoreCase(str)) {  
                ipString = str;  
                break;  
            }  
        }  
        
        if(StringUtils.isBlank(ipString)) {
        	ipString = "unknown ip";
        }
        
        return ipString;  
    }

    /**
     * 通过GET_URL调用接口获取内容
     */
    public static String readContentFromGet(String GET_URL) throws IOException {
        String result = null;
        BufferedReader reader = null;
        HttpURLConnection connection = null;

        try{
            String getURL =  GET_URL;
            URL getUrl = new URL(getURL);

            // 根据拼凑的URL，打开连接，URL.openConnection函数会根据URL的类型，
            connection = (HttpURLConnection) getUrl.openConnection();
            connection.setConnectTimeout(30000);
            connection.setReadTimeout(30000);
            connection.setRequestProperty("Accept-Charset", "UTF-8");
            connection.setRequestProperty("contentType", "UTF-8");
            connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");

            // 进行连接
            connection.connect();
            // 取得输入流，并使用Reader读取
            reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));

            String line = null;
            StringBuffer lines = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                lines.append(line);
            }

            result = lines.toString();
        } catch (Exception e) {
            System.out.println("GET_URL Result Exception : "+e.getMessage());
        } finally {
            // 释放资源 断开连接
            IOUtils.closeQuietly(reader);
            connection.disconnect();
            connection = null;
        }
        return result;
    }
	
}
